package praktikumid.w11.p3;


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;


public class UndoWithAction {
	/**
	 * Simple element, which technically only has
	 * one String - name. 
	 * @author Ago
	 *
	 */
	static class Element {
		/**
		 * Name of the element.
		 */
		String name;
		
		boolean isVisible = true;
		
		/**
		 * Constructor, where we set the name.
		 * @param name
		 */
		public Element(String name) {
			this.name = name;
		}
		
		public Element(Element copy) {
			name = copy.name;
		}
		
		@Override
		public String toString() {
			// let's have a "nice" printout
			return name;
		}
	}
	
	static class Action {
		static final int ACTION_ADD = 1;
		static final int ACTION_MODIFY = 2;
		static final int ACTION_REMOVE = 3;
		int type = ACTION_ADD;
		
		// reference to element in the list
		Element e;
		// used only in case of modifying
		Element startElement;
		Element endElement;
		
		public Action(Element e2) {
			e = e2;
			if (!e2.isVisible) {
				type = ACTION_REMOVE;
			}
			//this.e = e;
		}
		
		public Action(Element startElement, Element endElement) {
			e = endElement;
			this.startElement = new Element(startElement);
			// same as:
			// this.startElement = new Element();
			// this.startElement.name = startElement.name;
			this.endElement = new Element(endElement);
			type = ACTION_MODIFY;
		}
		
		public void undo() {
			switch (type) {
			case ACTION_ADD:
				e.isVisible = false;
				break;
			case ACTION_MODIFY:
				e.name = startElement.name;
				break;
			case ACTION_REMOVE:
				e.isVisible = true;
				break;
			}
		}
		
	}
	
	/**
	 * A list of elements.
	 */
	static List<Element> elements = new ArrayList<Element>();
	
	static List<Action> actions = new ArrayList<Action>();
	static int actionIndex = -1;

	/**
	 * Main.
	 * @param args arguments.
	 * @throws IOException Dealing with input can throw exception.
	 */
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(
				new InputStreamReader(System.in));
		while(true) {
			printHelp();
			String line = br.readLine();
			// we expect correct input
			// just split everything by space
			String[] tokens = line.split(" ");
			int op = Integer.valueOf(tokens[0]);
			int elid = -1;
			Element e;
			if (op > 0 && op < 4) {
				actionIndex++;
				while (actionIndex < actions.size()) {
					actions.remove(actionIndex);
				}
			}
			switch (op) {
			case 1: // add element
				e = new Element(tokens[1]);
				elements.add(e);
				actions.add(new Action(e));
				break;
			case 2: // modify element, rename
				elid = Integer.valueOf(tokens[1]);
				e = elements.get(elid);
				Element old = new Element(e);
				e.name = tokens[2];
				actions.add(new Action(old, e));
				break;
			case 3: // delete element
				elid = Integer.valueOf(tokens[1]);
				e = elements.get(elid);
				//elements.remove(elid);
				e.isVisible = false;
				actions.add(new Action(e));
				break;
			case 4: // printout
				printElements();
				break;
			case 5:
				undo();
				printElements();
				break;
			case 0:
				System.exit(0);
			}

			
		}
		
	}
	
	public static void undo() {
		if (actionIndex >= 0) {
			Action a = actions.get(actionIndex);
			a.undo();
			actionIndex--;
		}
	}
	
	/**
	 * Prints out the elements in the list.
	 */
	public static void printElements() {
		System.out.println("Elements:");
		for (int i = 0; i < elements.size(); i++) {
			if (elements.get(i).isVisible) {
				System.out.println(i + ". " + elements.get(i));
			}
		}
	}
	
	/**
	 * Prints out help - what can be done with the program.
	 */
	public static void printHelp() {
		System.out.println("  1 add element [1 name]");
		System.out.println("  2 change element [2 elid name]");
		System.out.println("  3 delete element [3 elid]");
		System.out.println("  4 list elements [4]");
		System.out.println("  5 undo [5]");
		System.out.println("  0 exit [0]");
	}
}
